﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FrmResult : Form
    {
        SqlConnection conn;
        public static string Stu_Code1;
        public FrmResult()
        {
            InitializeComponent();
        }
        private void LoadResult()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select TblStu_Sub.Student_Code,TblSubject.Subject_Code,TblSubject.Subject_Name,TblSubject.Term,TblSubject.No_Of_Credit,TblStu_Sub.Take_Time,TblStu_Sub.Mark from TblSubject,TblStu_Sub where TblSubject.Subject_Code=TblStu_Sub.Subject_Code and TblStu_Sub.Student_Code='" + FormInfo_Student.student_code + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgResult.DataSource = dt;
            conn.Close();
        }

        private void FrmResult_Load(object sender, EventArgs e)
        {
            LoadResult();
            //string Stu_Code = dgResult.CurrentRow.Cells[0].Value.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Stu_Code1 = dgResult.CurrentRow.Cells[0].Value.ToString();
            FormAdd_Mark FormAdd_Mark = new FormAdd_Mark();
            FormAdd_Mark.Show();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string stucode = dgResult.CurrentRow.Cells[0].FormattedValue.ToString();
            string subcode = dgResult.CurrentRow.Cells[1].FormattedValue.ToString();
            string mark = dgResult.CurrentRow.Cells[6].FormattedValue.ToString();
            conn = Connect.getConnection();
            conn.Open();
            string sql = "update TblStu_Sub set Mark='" + mark + "'where Student_Code= '" + stucode + "' and Subject_Code='" + subcode + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            MessageBox.Show("Mark is updated");
            conn.Close();
            LoadResult();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "You want to delete this mark?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                string stucode = dgResult.CurrentRow.Cells[0].FormattedValue.ToString();
                string subcode = dgResult.CurrentRow.Cells[1].FormattedValue.ToString();
                conn = Connect.getConnection();
                conn.Open();
                string sql = "delete from TblStu_Sub where Student_Code= '" + stucode + "' and Subject_Code='" + subcode + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                LoadResult();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

    }
}
